<template>
    <div>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <el-button v-perms="['content.post/delete']" :disabled="!selectData.length" @click="handleDelete(selectData)">
                删除
            </el-button>
            <div class="mt-4">
                <el-table :data="pager.lists" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" />
                    <el-table-column label="ID" prop="id" show-overflow-tooltip />
                    <el-table-column label="内容" prop="content" show-overflow-tooltip />
                    <el-table-column label="浏览量" prop="see_num" show-overflow-tooltip />
                    <el-table-column label="点赞数" prop="thumb_num" show-overflow-tooltip />
                    <el-table-column label="评论数" prop="comment_num" show-overflow-tooltip />
                    <el-table-column label="发布时间" prop="create_time" show-overflow-tooltip />

                    <el-table-column label="操作" width="120" fixed="right">
                        <template #default="{ row }">
                            <el-button v-perms="['content.post/delete']" type="danger" link @click="handleDelete(row.id)">
                                删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex mt-4 justify-end">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
    </div>
</template>

<script lang="ts" setup name="postLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import { apiPostLists, apiPostDelete } from '@/api/post'
import feedback from '@/utils/feedback'

// 选中数据
const selectData = ref<any[]>([])

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
    selectData.value = val.map(({ id }) => id)
}

// 获取字典数据
const { dictData } = useDictData('')

const queryParams = reactive({
    title: '',
    cid: '',
    is_show: ''
})

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiPostLists,
    params: queryParams
})

// 删除
const handleDelete = async (id: number | any[]) => {
    await feedback.confirm('确定要删除？')
    await apiPostDelete({ id })
    getLists()
}

getLists()
</script>